package com.googlecode.openauth.web.action;

import java.util.List;

import com.googlecode.openauth.web.dao.IActionDao;
import com.googlecode.openauth.web.entity.ActionInfo;
import com.opensymphony.xwork2.ActionSupport;

public class ActionInfoAction extends ActionSupport{
	private IActionDao actionDao;
	private int actionId;
	private String uuid;
	private String actionName;
	private String actionDesc;
	private int[] actionIds;
	private List<ActionInfo> list;
	private String oper="list";
	
	public String execute() throws Exception{
		//TODO
		return null;
	}
	
	public String save() throws Exception {
		if(oper.equals("save")) {
			actionDao.addAction(this.getActionFromForm());
			resetForm();
			return "success";
		} else {
			actionDao.updateAction(this.getActionFromForm());
			resetForm();
			this.actionId = 0;
			return "udpate";
		}
		
	}
	public String init() throws Exception{
		if(oper.equals("save")) {
			return "new";
		} else {
			ActionInfo action = actionDao.getActionById(actionId);
			setActionFromForm(action);
			return "update";
		}
	}
	public String delete() throws Exception{
		actionDao.delActionById(actionIds[0]);
		return "success";
	}
	
	public int getAllCount() throws Exception{
		int count = 0;
		//int count = actionDao.getAllActionsCount();
		return count;
	}
	
	public ActionInfo getActionFromForm() {
		ActionInfo action = new ActionInfo();
		if (uuid != null && !uuid.isEmpty())
			action.setUuid(uuid);
		action.setActionName(actionName);
		action.setActionDesc(actionDesc);
		return action;
	}
	
	public void setActionFromForm(ActionInfo action) {
		uuid = action.getUuid();
		actionName = action.getActionName();
		actionDesc = action.getActionDesc();
	}
	
	public void resetForm() {
		uuid="";
		actionName = "";
		actionDesc = "";
	}
	
	public IActionDao getActionDao() {
		return actionDao;
	}
	public void setActionDao(IActionDao authDao) {
		this.actionDao = authDao;
	}
	
	public String getActionName() {
		return actionName;
	}
	
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	
	public String getActionDesc() {
		return actionDesc;
	}
	
	public void setActionDesc(String actionDesc) {
		this.actionDesc = actionDesc;
	}
	public List<ActionInfo> getList() {
		return list;
	}
	
	public void setList(List<ActionInfo> list) {
		this.list = list;
	}
	
	public String getOper() {
		return oper;
	}
	public void setOper(String oper) {
		this.oper = oper;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public int getActionId() {
		return actionId;
	}

	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

	public int[] getActionIds() {
		return actionIds;
	}

	public void setActionIds(int[] actionIds) {
		this.actionIds = actionIds;
	}
}
